package mis510.homefinders;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

public class Map extends Activity{
	 static String search_address = null;
	 Button bk_btn;
	 Intent nextActivity;
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.map);
	        System.out.println("map");
	        Intent intent = this.getParent().getIntent();
			Bundle bundle = intent.getExtras();
			search_address = bundle.getString("address");
			System.out.println("passed address in map tab: "+search_address);
			
	        WebView myWebView = (WebView) findViewById(R.id.webView_map);
	        WebSettings webSettings = myWebView.getSettings();
	        webSettings.setJavaScriptEnabled(true);
	        webSettings.setBuiltInZoomControls(false);
	        myWebView.setWebChromeClient(new WebChromeClient());
	        final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this);  
	        myWebView.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
	        myWebView.loadUrl("http://54.214.11.178:8080/HomeFinders/map.html");
	        
	        bk_btn = (Button)findViewById(R.id.bk_btn);
	        bk_btn.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View view) {
	            	nextActivity = new Intent(Map.this, SearchProperty.class);
	            	startActivity(nextActivity);
	            }
	        });
	    }
	 
	 public class JavaScriptInterface {
			Context mContext;
			Map address_map = new Map();
	 		String address_send = address_map.search_address;
		    JavaScriptInterface(Context c) {
		        mContext = c;
		    }
		    @JavascriptInterface
		    public String getAddress(){	    	
		    	//final String msgeToast = webMessage;
		    	System.out.println("address_send: "+address_send);
		    	return address_send;
//		    	 myHandler.post(new Runnable() {
//		             @Override
//		             public void run() {
//		                 // This gets executed on the UI thread so it can safely modify Views
//		                 myTextView.setText(msgeToast);
//		             }
//		         });

		       //Toast.makeText(mContext, webMessage, Toast.LENGTH_SHORT).show();
		    }
	    }
	 	

}
